package com.sh.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sh.common.core.utils.MapstructUtils;
import com.sh.common.core.utils.StringUtils;
import com.sh.common.mybatis.core.page.TableDataInfo;
import com.sh.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sh.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sh.business.domain.bo.FeedbackBo;
import com.sh.business.domain.vo.FeedbackVo;
import com.sh.business.domain.Feedback;
import com.sh.business.mapper.FeedbackMapper;
import com.sh.business.service.IFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-11
 */
@RequiredArgsConstructor
@Service
public class FeedbackServiceImpl implements IFeedbackService {

    private final FeedbackMapper baseMapper;

    /**
     * 查询用户反馈
     *
     * @param feedbackId 主键
     * @return 用户反馈
     */
    @Override
    public FeedbackVo queryById(Long feedbackId){
        return baseMapper.selectVoById(feedbackId);
    }

    /**
     * 分页查询用户反馈列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户反馈分页列表
     */
    @Override
    public TableDataInfo<FeedbackVo> queryPageList(FeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Feedback> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(Feedback::getCreateTime);
        Page<FeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<FeedbackVo> queryPortalPageList(FeedbackBo bo, PageQuery pageQuery) {
        bo.setCreateBy(LoginHelper.getUserId());
        LambdaQueryWrapper<Feedback> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(Feedback::getCreateTime);
        Page<FeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户反馈列表
     *
     * @param bo 查询条件
     * @return 用户反馈列表
     */
    @Override
    public List<FeedbackVo> queryList(FeedbackBo bo) {
        LambdaQueryWrapper<Feedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Feedback> buildQueryWrapper(FeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Feedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackContent()), Feedback::getFeedbackContent, bo.getFeedbackContent());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackImg()), Feedback::getFeedbackImg, bo.getFeedbackImg());
        lqw.eq(StringUtils.isNotBlank(bo.getReplayContent()), Feedback::getReplayContent, bo.getReplayContent());
        lqw.eq(bo.getStatus() != null, Feedback::getStatus, bo.getStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getCreateBy()), Feedback::getCreateBy, bo.getCreateBy());
        return lqw;
    }

    /**
     * 新增用户反馈
     *
     * @param bo 用户反馈
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(FeedbackBo bo) {
        Feedback add = MapstructUtils.convert(bo, Feedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFeedbackId(add.getFeedbackId());
        }
        return flag;
    }

    /**
     * 修改用户反馈
     *
     * @param bo 用户反馈
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(FeedbackBo bo) {
        Feedback update = MapstructUtils.convert(bo, Feedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Feedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户反馈信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
